﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace Platformer
{
    public delegate void OnClick();
    class GUIButton
    {
        public Rectangle Bounds { get; set; }

        public Vector2 Position { get { return new Vector2(Bounds.X, Bounds.Y); }
            set { Bounds = new Rectangle((int)value.X, (int)value.Y, Bounds.Width, Bounds.Height); }
        }

        public Texture2D Icon { get; set; }

        public OnClick OnClick { get; set; }

        private MouseState previousMouseState;

        public GUIButton()
        {
            previousMouseState = Mouse.GetState();
        }


        public void Update(GameTime gameTime)
        {
            MouseState currentMouseState = Mouse.GetState();

            bool mouseInsideButton = Bounds.Contains(currentMouseState.X, currentMouseState.Y);



            bool mouseClicked = (previousMouseState.LeftButton == ButtonState.Pressed &&
                                 currentMouseState.LeftButton == ButtonState.Released);

            if (mouseInsideButton && mouseClicked)
            {
                OnClick();
            }

            previousMouseState = currentMouseState;
        }

        public void Draw(SpriteBatch sb)
        {
            sb.Draw(Icon,this.Position, Color.Red);
        }
    }
}
